﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainApp
{
    
    public partial class Okno : Form
    {
        string CMD_FOR_SEND;//command for send to an thing
        controller pilot = new controller();
        string LOCATION = AppDomain.CurrentDomain.BaseDirectory;
        public Okno()
        {   
            InitializeComponent();
            try
            {
                console_log.ReadXml(LOCATION + "log.xml");
            }
            catch (System.IO.FileNotFoundException)
            {
                DataRow TMPdatarow = log_usr.NewRow();
                TMPdatarow["log_usr_time"] = System.DateTime.UtcNow;
                TMPdatarow["log_usr_ft"] = "PC->BOAT";
                TMPdatarow["log_usr_text"] = "First Start";
                log_usr.Rows.Add(TMPdatarow);
                console_log.WriteXml(LOCATION + "log.xml");

            }
            loadsettings();
 


            
        }


        //action generated by refreshtimer 
        private void refreshTick(object sender, EventArgs e)
        {
            pilot.refresh();
            comands();
           
        }

        //sending comands and receiving logs
        private void comands()
        {
            //należy dodać konkretne funkcje odpowiadające za komunikację, na razie będzie jakaś bezsensowan demonstracja z dev random

             if(CMD_FOR_SEND!=null)
             {
                 DataRow TMPdatarow = log_usr.NewRow();
                 TMPdatarow["log_usr_time"] = System.DateTime.UtcNow;
                 TMPdatarow["log_usr_ft"] = "PC->BOAT";
                 TMPdatarow["log_usr_text"] = CMD_FOR_SEND;
                 log_usr.Rows.Add(TMPdatarow );
                 CMD_FOR_SEND = null;

                 console_list.FirstDisplayedScrollingRowIndex = console_list.RowCount - 10;
             }



        }


        private void click_exit(object sender, EventArgs e)
        {

            Close();
        }

        private void click_minimize(object sender, EventArgs e)
        {
            
            this.WindowState = FormWindowState.Minimized;
        }

        private void click_restore(object sender, EventArgs e)
        {

            if(this.WindowState==FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;

            }
        }


        private void Okno_Resize(object sender, EventArgs e)
        {
            Sizes();
        }

        private void map_init()
        {
            
        }

        private void Sizes()//set new sizes of elements
        {
            tableLayoutPanel1.Width = Okno.ActiveForm.Width - 16;
            tableLayoutPanel1.Height = Okno.ActiveForm.Height - 78;
            int tW, tH;
            tW = tableLayoutPanel1.Width / 2;
            tH = tableLayoutPanel1.Height / 2;

            tabLG.Width = tW;
            tabLG.Height = tH;

            tabLD.Width = tW;
            tabLD.Height = tH;

            tabPG.Width = tW;
            tabPG.Height = tH;

            tabPD.Width = tW;
            tabPD.Height = tH;


            console_command.Location = new System.Drawing.Point(5, tp_log.Height - console_command.Height);
            console_command.Width = tp_log.Width - console_exe.Width-10;

            console_exe.Location = new System.Drawing.Point(console_command.Width + 5, tp_log.Height - console_exe.Height);

            console_list.Height = tp_log.Height - 30;
            console_list.Width = tp_log.Width - console_list.Location.X;

            mapa.Location = new System.Drawing.Point(5, 5);
            mapa.Height = tH - 30;
            mapa.Width = tW - 30;

            

            


        }

        private void Okno_Load(object sender, EventArgs e)
        {
            Sizes();
            map_init();
        }

        private void tabLG_MouseMove(object sender, MouseEventArgs e)
        {
            if (tabLG.TabCount > 1)
            { if (e.Button == MouseButtons.Left)
                this.tabLG.DoDragDrop(this.tabLG.SelectedTab, DragDropEffects.All);
            }
            
        }

        private void tabPG_MouseMove(object sender, MouseEventArgs e)
        {
            if (tabPG.TabCount>1)
            {
                if (e.Button == MouseButtons.Left)
                    this.tabPG.DoDragDrop(this.tabPG.SelectedTab, DragDropEffects.All);
            }
        }

        private void tabLD_MouseMove(object sender, MouseEventArgs e)
        {
            if (tabLD.TabCount > 1)
            {
                if (e.Button == MouseButtons.Left)
                    this.tabLD.DoDragDrop(this.tabLD.SelectedTab, DragDropEffects.All);
            }
        }

        private void tabPD_MouseMove(object sender, MouseEventArgs e)
        {
            if (tabPD.TabCount > 1)
            {
                if (e.Button == MouseButtons.Left)
                    this.tabPD.DoDragDrop(this.tabPD.SelectedTab, DragDropEffects.All);
            }
        }

        private void tabPD_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None; 
        }

        private void tabPG_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None; 
        }

        private void tabLD_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None; 
        }

        private void tabLG_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None; 
        }

        private void tabLG_DragDrop(object sender, DragEventArgs e)
        {
            TabPage DropTab = (TabPage)(e.Data.GetData(typeof(TabPage)));
            if (tabLG.SelectedTab != DropTab)
                this.tabLG.TabPages.Add(DropTab); 
        }

        private void tabPG_DragDrop(object sender, DragEventArgs e)
        {
            TabPage DropTab = (TabPage)(e.Data.GetData(typeof(TabPage)));
            if (tabPG.SelectedTab != DropTab)
                this.tabPG.TabPages.Add(DropTab); 
        }

        private void tabLD_DragDrop(object sender, DragEventArgs e)
        {
            TabPage DropTab = (TabPage)(e.Data.GetData(typeof(TabPage)));
            if (tabLD.SelectedTab != DropTab)
                this.tabLD.TabPages.Add(DropTab); 
        }

        private void tabPD_DragDrop(object sender, DragEventArgs e)
        {
            TabPage DropTab = (TabPage)(e.Data.GetData(typeof(TabPage)));
            if (tabPD.SelectedTab != DropTab)
                this.tabPD.TabPages.Add(DropTab); 
        }

        private void console_exe_Click(object sender, EventArgs e)
        {
            CMD_FOR_SEND = console_command.Text;
            console_command.Text = null;

        }

        private void Okno_FormClosing(object sender, FormClosingEventArgs e)
        {
            console_log.WriteXml(LOCATION + "log.xml");
        }

        private void console_command_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter )
            {
                console_exe_Click(sender, e);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Form form = new settings() ;
            form.Show();
            form.FormClosed += form_FormClosed;
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            loadsettings();
            //throw new NotImplementedException();
        }

        void loadsettings()
        {
            try
            {
                ds_settings.ReadXml(LOCATION + "settings.xml");
            }
            catch (System.IO.FileNotFoundException)
            {
                MessageBox.Show("any setting file, add new settings in settings window");

            }

        }

        private void meter1_Load(object sender, EventArgs e)
        {
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            float num;
            string str=textBox1.Text;
            if (float.TryParse(str,out num))
            {
                meter1.value =  num;
                meter1.refresh();
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

    }
}
